import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Begin {
    public static void main(String[] args) {
        PersonalFinanceManager manager = new PersonalFinanceManager();
        Scanner scanner = new Scanner(System.in);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        while (true) {
            System.out.println("\n1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 查询交易记录");
            System.out.println("4. 按类别查询交易记录");
            System.out.println("5. 按日期范围查询交易记录");
            System.out.println("6. 获取总收入");
            System.out.println("7. 获取总支出");
            System.out.println("8. 获取净余额");
            System.out.println("9. 退出");
            System.out.print("请选择一个选项: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            try {
                switch (choice) {
                    case 1:
                        System.out.print("请输入日期 (yyyy-MM-dd): ");
                        Date incomeDate = sdf.parse(scanner.nextLine());
                        System.out.print("请输入金额: ");
                        double incomeAmount = scanner.nextDouble();
                        scanner.nextLine(); // 消耗换行符
                        System.out.print("请输入类别: ");
                        String incomeCategory = scanner.nextLine();
                        System.out.print("请输入备注: ");
                        String incomeNote = scanner.nextLine();
                        manager.recordIncome(incomeDate, incomeAmount, incomeCategory, incomeNote);
                        break;
                    case 2:
                        System.out.print("请输入日期 (yyyy-MM-dd): ");
                        Date expenseDate = sdf.parse(scanner.nextLine());
                        System.out.print("请输入金额: ");
                        double expenseAmount = scanner.nextDouble();
                        scanner.nextLine(); // 消耗换行符
                        System.out.print("请输入类别: ");
                        String expenseCategory = scanner.nextLine();
                        System.out.print("请输入备注: ");
                        String expenseNote = scanner.nextLine();
                        manager.recordExpense(expenseDate, expenseAmount, expenseCategory, expenseNote);
                        break;
                    case 3:
                        manager.queryTransactions();
                        break;
                    case 4:
                        System.out.print("请输入类别: ");
                        String category = scanner.nextLine();
                        manager.queryTransactionsByCategory(category);
                        break;
                    case 5:
                        System.out.print("请输入开始日期 (yyyy-MM-dd): ");
                        Date startDate = sdf.parse(scanner.nextLine());
                        System.out.print("请输入结束日期 (yyyy-MM-dd): ");
                        Date endDate = sdf.parse(scanner.nextLine());
                        manager.queryTransactionsByDateRange(startDate, endDate);
                        break;
                    case 6:
                        System.out.println("总收入: " + manager.getTotalIncome());
                        break;
                    case 7:
                        System.out.println("总支出: " + manager.getTotalExpense());
                        break;
                    case 8:
                        System.out.println("净余额: " + manager.getNetBalance());
                        break;
                    case 9:
                        System.out.println("正在退出...");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选项，请重试。");
                }
            } catch (ParseException e) {
                System.out.println("日期格式错误，请输入正确的日期格式 (yyyy-MM-dd)。");
            }
        }
    }
}